// pages/payment/payment.js
var app = getApp()
var util = require('../../utils/util.js')
var host = app.globalData.host
var self = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    mobile: '',
    code: '',
    price1Count: 0,
    price2Count: 0,
    price3Count: 0,
    peoples: [],
    price1Index: 0,
    ticketId: '',
    info: null,
    total: 0,
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    notice: '',
  },
  getStyle() {
    wx.setNavigationBarTitle({
      title: '购票'
    })
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  getInfo() {
    app.request({
      url: host + '/xhc/ticket/info',
      data: {
        id: self.data.ticketId
      },
      success: function (res) {
        self.setData({
          info: res.data.info
        })
        self.setData({
          price1Count: 1
        })
        self.counter()
      }, fail: function (msg) {
        console.warn(msg)
      }
    })
  },
  changeName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  changePhone(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  changeCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  price1Change(e) {
    var price1Count = parseInt(e.detail.value)
    self.setData({
      price1Count: price1Count
    })
    self.counter()
  },
  price2Change(e) {
    var price2Count = parseInt(e.detail.value);
    self.setData({
      price2Count: price2Count
    })
    self.counter()
  },
  price3Change(e) {
    var price3Count = parseInt(e.detail.value)
    self.setData({
      price3Count: price3Count
    })
    self.counter()
  },
  decrease1(e) {
    var price1Count = parseInt(e.currentTarget.dataset.value)
    if (price1Count > 0) {
      price1Count -= 1
      self.setData({
        price1Count: price1Count
      })
    }
    self.counter()
  },
  plus1(e) {
    var price1Count = parseInt(e.currentTarget.dataset.value)
    price1Count += 1
    self.setData({
      price1Count: price1Count
    })
    self.counter()
  },
  decrease2(e) {
    var price2Count = parseInt(e.currentTarget.dataset.value)
    if (price2Count > 0) {
      price2Count -= 1
      self.setData({
        price2Count: price2Count
      })
    }
    self.counter()
  },
  plus2(e) {
    var price2Count = parseInt(e.currentTarget.dataset.value)
    price2Count += 1
    self.setData({
      price2Count: price2Count
    })
    self.counter()
  },
  decrease3(e) {
    var price3Count = parseInt(e.currentTarget.dataset.value)
    if (price3Count > 0) {
      price3Count -= 1
      self.setData({
        price3Count: price3Count
      })
    }
    self.counter()
  },
  plus3(e) {
    var price3Count = parseInt(e.currentTarget.dataset.value)
    price3Count += 1
    self.setData({
      price3Count: price3Count
    })
    self.counter()
  },
  counter() {
    var price1Count = self.data.price1Count
    var price1 = self.data.info.price1

    var price2Count = self.data.price2Count
    var price2 = self.data.info.price2

    var price3Count = self.data.price3Count
    var price3 = self.data.info.price3

    var total = price1Count * price1 + price2Count * price2 + price3Count * price3
    var totalF = total.toFixed(2)
    self.setData({
      total: totalF
    })
  },
  onSubmit() {
    if (!util.validateData(app.globalData.openId)) {
      wx.showModal({
        title: '提示',
        content: '还没允许获取用户信息',
        confirmText: '去设置',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
              }
            })
          }
        }
      })
      return;
    }
    if (!util.validateData(self.data.name)) {
      util.showModel('请输入姓名')
      return;
    }
    if (!util.validateData(self.data.mobile)) {
      util.showModel('请输入手机号码')
      return;
    }
    // if (self.data.total <= 0) {
    //   util.showModel('请选择一张票')
    //   return;
    // }
    if (!util.validateData(self.data.code)) {
      util.showModel('请输入验证码')
      return;
    }
    wx.showLoading({
      title: '提交中',
    })
    app.request({
      url: host + '/xhc/ticket/pay',
      data: {
        name: self.data.name,
        mobile: self.data.mobile,
        code: self.data.code,
        price1Count: self.data.price1Count,
        price2Count: self.data.price2Count,
        price3Count: self.data.price3Count,
        ticketId: self.data.ticketId
      },
      success: function (res) {
        wx.hideLoading()
        var status = res.data.status
        if (status == 204) {
          util.showModel(res.data.msg)
          return;
        }
        var response = res.data.info
        var nonce_str = response.nonce_str
        var prepay_id = response.result.prepay_id
        var sign = response.sign
        var timestamp = response.timeStamp
        var appId = response.appid
        var param = {
          'appId': appId,
          'timeStamp': '' + timestamp,
          'nonceStr': '' + nonce_str,
          'package': 'prepay_id=' + prepay_id,
          'signType': 'MD5',
          'paySign': '' + sign,
          'success': function (res) {
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/success/success?isShowContent=' + 1
              })
            }, 200)

          },
          'fail': function (res) {
            util.showModel('支付失败')
          }
        }
        wx.requestPayment(param)

      }, fail: function (res) {
        wx.hideLoading()
        util.showModel(res.data.msg)
      }
    })
  },
  RequstCode() {
    if (!util.validateData(self.data.mobile)) {
      util.showModel('电话号码不能为空')
      return;
    }
    app.request({
      url: host + '/xhc/ticket/code',
      data: {
        mobile: self.data.mobile
      },
      success: function (res) {
        util.showModel('验证码已发送到手机')
      }, fail: function (res) {
        util.showModel(res.data.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this
    var temp = []
    for (var i = 0; i <= 10; i++) {
      temp.push(i)
    }
    this.setData({
      peoples: temp,
      ticketId: options.ticketId,
      notice: app.globalData.notice
    })
    this.getInfo()
    this.getStyle()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})